package qmm.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import qmm.dao.OrderInfoDao;
import qmm.entity.Goods;
import qmm.entity.OrderInfo;
import qmm.entity.vo.OrderVo;
import qmm.util.JdbcUtil;

public class OrderInfoDaoImpl implements OrderInfoDao {

	@Override
	public int add(OrderInfo info) {
		String sql = "INSERT INTO t_order_info (base_order_id, goods_id, buy_num, amount) VALUES ( "+info.getBaseOrderId()+", "+info.getGoodsId()+", "+info.getBuyNum()+", "+info.getAmount()+")";
		return JdbcUtil.update(sql);
	}

	@Override
	public List<OrderVo> findOrder(int orderId) {
		List<OrderVo> list = new ArrayList<OrderVo>();
		String sql = "select b.order_no,g.id,g.goods_name,g.file_name,g.price,i.buy_num,i.amount from t_base_order b inner join t_order_info i on b.id=i.base_order_id inner join t_goods g on i.goods_id=g.id where b.id="+orderId;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int goodsId = rs.getInt("id");
				String orderNo = rs.getString("order_no");
				String goodsName = rs.getString("goods_name");
				String fileName = rs.getString("file_name");
				double price = rs.getDouble("price");
				int buyNum = rs.getInt("buy_num");
				double amount = rs.getDouble("amount");
				OrderVo vo = new OrderVo(orderNo,goodsId, goodsName, fileName, price, buyNum, amount);
				list.add(vo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

}